﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using UnityEngine.AI;
using UnityEngine.Events;

//角色移动以及检测
public class PlayerController : MonoBehaviour
{
    //定义一个Map类
    private MapCreator BoxMap;
    
    //场景的总数
    private int allSceneCount;

    //是否
    public bool isWin = false;
    public bool isDead = false;
    [SerializeField]
    private bool isMoving = false;
    
    
    //移动的速度,目标
    public float speed = 0.2f;
    private float aimX;
    private float aimZ;

    //拾取砖块的部分
    public GroundPicker groundPicker;


    //可以点击的图层
    public LayerMask clickAble;

    

    public GameObject dirLight;

    void Start()
    {
        //加载场景的总数
        //allSceneCount = SceneManager.sceneCountInBuildSettings;
        //获得类型为 MapCreator 的已加载的激活对象
        BoxMap = FindObjectOfType<MapCreator>();
        //agent = GetComponent<NavMeshAgent>();
        aimX = transform.position.x;
        aimZ = transform.position.z;
        dirLight = FindObjectOfType<Light>().gameObject;
        isWin = false;
        isDead = false;
        isMoving = false;
    }
    void Update()
    {
        checkShadowDir();
        checkClickable();
        if(transform.position.x!=aimX+0.5f||transform.position.z!=aimZ-0.5f)
        {
            isMoving = true;
        }
        else
        {
            isMoving = false;
        }
        transform.position = Vector3.MoveTowards(transform.position, new Vector3(aimX + 0.5f, transform.position.y, aimZ-0.5f), speed * Time.deltaTime);
    }

    //检查玩家是否踩到阴影，应该是在玩家运动的过程中慢慢检查
    void checkClickable()
    {
        RaycastHit hit;
        if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),out hit,50,clickAble.value))
        {
            Vector3 move = hit.collider.gameObject.transform.position;
            if(Input.GetMouseButtonDown(0))
            {
                aimX = move.x;
                aimZ = move.z;
            }
            if(Input.GetMouseButtonDown(1))
            {
                if(!isMoving&&Vector3.Distance(hit.transform.position,transform.position)<=2&&Mathf.Abs(hit.transform.position.y-transform.position.y)<=0.2)
                {
                    if (groundPicker.IsPicked)
                    {
                        hit.collider.gameObject.transform.parent = null;
                        groundPicker.IsPicked = !groundPicker.IsPicked;
                    }
                    else
                    {
                        groundPicker.ground = hit.collider.gameObject;
                        hit.collider.gameObject.transform.parent = transform;
                        groundPicker.IsPicked = !groundPicker.IsPicked;
                    }
                }
            }
        }
    }

    void checkShadowDir()
    {
        Light light = dirLight.GetComponent<Light>();
        RaycastHit hit;
        Ray ray = new Ray(transform.position, -light.transform.forward);
        if (Physics.Raycast(ray, out hit,30, clickAble.value))
        {
            Debug.DrawLine(transform.position, hit.transform.position, Color.red);
        }
        else
        {
            isDead = true;
        }
    }



    /// <summary>
    /// 玩家移动，并检测
    /// </summary>
    void Move(int moveX,int moveZ)
    {

        //玩家的下个位置
        int nextX = moveX + ( int ) transform.position.x;
        int nextZ = moveZ + ( int ) transform.position.z;

        if ( isMove( moveX , moveZ ) )
        {
            //玩家移动到下一个位置
            transform.position = new Vector3( nextX , 0.5f , nextZ );
            //SceneContral.Instance.walkCount++;
        }

    }
    /// <summary>
    /// 根据x,z,坐标判断是否是墙
    /// </summary>
    /// <param name="x"></param>
    /// <param name="z"></param>
    /// <returns></returns>
    bool IsWall( int x , int z )
    {
        return BoxMap.GetWallPosSet().Contains( BoxMap.TwoDToOneD( x , z ) );
    }
    /// <summary>
    ///  根据x,z,坐标判断是否是盒子
    /// </summary>
    /// <param name="x"></param>
    /// <param name="z"></param>
    /// <returns></returns>
    bool IsBox( int x , int z )
    {
        return BoxMap.GetPosBoxMap().ContainsKey( BoxMap.TwoDToOneD( x , z ) );
    }
    /// <summary>
    /// 根据x,y位置得到盒子的GameObject
    /// </summary>
    /// <param name="x"></param>
    /// <param name="z"></param>
    /// <returns></returns>
    GameObject GetBox( int x , int z )
    {
        return BoxMap.GetPosBoxMap()[BoxMap.TwoDToOneD( x , z )];
    }
    /// <summary>
    /// 判断玩家是否移动
    /// </summary>
    /// <param name="x"> 水平偏移量</param>
    /// <param name="z"> 锤子偏移量</param>
    /// <returns></returns>
    bool isMove( int x , int z )
    {
        if ( x == 0 && z == 0 )
        {
            return false;
        }
        else return true;
    }

}

[System.Serializable]
public class EventVector3 : UnityEvent<Vector3> { }